<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
</head>
<body>
<div class="addBrand-container" id="member-add-app">
    <div class="container">
        <el-form
                ref="ruleForm"
                :model="ruleForm"
                :rules="rules"
                :inline="false"
                label-width="180px"
                class="demo-ruleForm"
        >
            <el-form-item
                    label="会员姓名:"
                    prop="memberName"
            >
                <el-input
                        v-model="ruleForm.memberName"
                        placeholder="请输入会员姓名"
                        maxlength="20"
                />
            </el-form-item>

            <el-form-item
                    label="手机号:"
                    prop="phone"
            >
                <el-input
                        v-model="ruleForm.phone"
                        placeholder="请输入手机号"
                        maxlength="20"
                />
            </el-form-item>
            <el-form-item
                    label="性别:"
                    prop="sex"
            >
                <el-radio-group v-model="ruleForm.sex">
                    <el-radio label="1">男</el-radio>
                    <el-radio label="0">女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item
                    label="会员生日:"
                    prop="birthday">
                <template>
                    <div class="block">
                        <el-date-picker
                                v-model="ruleForm.birthday"
                                type="date"
                                maxlength="20"
                                placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </template>
            </el-form-item>
            <div class="subBox address">
                <el-form-item>
                    <el-button @click="goBack()">
                        取消
                    </el-button>
                    <el-button
                            type="primary"
                            @click="submitForm('ruleForm', false)"
                    >
                        保存
                    </el-button>
                    <el-button
                            v-if="actionType === 'add'"
                            type="primary"
                            class="continue"
                            @click="submitForm('ruleForm', true)"
                    >
                        保存并继续添加
                    </el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vueMax.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/costAndMember.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script>
    new Vue({
        el: '#member-add-app',
        data() {
            return {
                id: '',
                actionType: '',
                ruleForm: {
                    'memberName': '',
                    'phone': '',
                    'sex': '',
                    'birthday': ''
                },
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    }
                },
            }
        },
        computed: {
            rules() {
                return {
                    //姓名
                    memberName: [{required: true, 'validator': checkName, 'trigger': 'blur'}],
                    'phone': [{'required': true, 'validator': checkPhone, 'trigger': 'blur'}],
                    'birthday': [{'required': true, 'message': '请选择会员生日', 'trigger': 'blur'}],
                }
            }
        },
        created() {
            this.id = requestUrlParam('id')
            this.actionType = this.id ? 'edit' : 'add'
        },
        mounted() {
        },
        methods: {
            submitForm(formName, st) {
                let isClear = true
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.actionType === 'add') {
                            const params = {
                                ...this.ruleForm,
                                birthday: this.dataFormatDate(this.ruleForm.birthday)
                            }
                            addMember(params).then(res => {
                                if (res.code === 1) {
                                    if (res.data == 1) {
                                        this.$message.success('会员添加成功 ~~')
                                    } else {
                                        this.$message.error('会员已存在，请检查手机号！')
                                        // 不退页面，不清除数据
                                        isClear = false
                                    }
                                    if (isClear) {
                                        if (st) {
                                            // 继续保存，不退页面，清除数据
                                            this.ruleForm = {
                                                'memberName': '',
                                                'phone': '',
                                                'sex': '',
                                                'birthday': ''
                                            }
                                        } else {
                                            // 不继续保存，退出页面
                                            this.goBack()
                                        }
                                    }
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                    } else {
                        return false
                    }
                })
            },
            goBack() {
                window.parent.menuHandle({
                    id: '2',
                    url: '/backend/page/member/list.html',
                    name: '会员管理'
                }, false)
            },
            dataFormatDate(dateString) {
                const date = new Date(dateString);
                const year = date.getFullYear();
                // 月份从 0 开始计数，所以要加 1
                const month = String(date.getMonth() + 1).padStart(2, '0');
                const day = String(date.getDate()).padStart(2, '0');
                return `${year}-${month}-${day}`;
            },
        }
    })
</script>
</body>
</html>
